#include <iostream>
#include <condition_variable>
#include <mutex>
#include <thread>

std::mutex mt;
std::condition_variable cv;
char curch = 'A';

void *printFunc(char ch, char nextch)
{
    for (int i = 0; i < 3; i++)
    {
        std::unique_lock<std::mutex> lock(mt);
        cv.wait(lock, [&]()
                { return ch == curch; });
        std::cout << ch;
        curch = nextch;
        // 唤醒其他线程
        cv.notify_all();
    }

    return NULL;
}

int main()
{
    std::thread s1(printFunc, 'A', 'B');
    std::thread s2(printFunc, 'B', 'C');
    std::thread s3(printFunc, 'C', 'A');

    s1.join();
    s2.join();
    s3.join();

    return 0;
}